import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import cart from '@/components/cart'
import item from '@/components/item'
import detail from '@/components/detail'
import mytest from '@/components/mytest'
import test from '@/components/test'
import register from '@/components/register'
import login from '@/components/login'
import myprofile from '@/components/myprofile'
import update_pass from '@/components/update_pass'
import picslist from '@/components/picslist'
import insertpics from '@/components/insertpics'
import update_pics from '@/components/update_pics'
import addgoods from '@/components/addgoods'
import editgood from '@/components/editgood'
import search from '@/components/search'

Vue.use(Router)

// 登录拦截器
var check_login = (to, from, next) => {
    if (localStorage.getItem('username')) {
        next()
    } else {
        next('/login')
    }
}


var routes = [
    {
        path: '/',
        name: 'index',
        component: index
    },
    {
        path: '/cart',
        name: 'cart',
        component: cart
    },
    {
        path: '/item',
        name: 'item',
        component: item
    },
    {
        path: '/detail',
        name: 'detail',
        component: detail
    },
    {
        path: '/mytest',
        name: 'mytest',
        component: mytest
    },
    {
        path: '/test',
        name: 'test',
        component: test
    },
    {
        path: '/reg',
        name: 'reg',
        component: register
    },
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/myprofile',
        name: 'myprofile',
        component: myprofile,
        beforeEnter: check_login
    },
    {
        path: '/update_pass',
        name: 'update_pass',
        component: update_pass,
        beforeEnter: check_login
    },
    {
        path: '/insertpics',
        name: 'insertpics',
        component: insertpics,
        beforeEnter: check_login
    },
    {
        path: '/picslist',
        name: 'picslist',
        component: picslist,
        beforeEnter: check_login
    },
    {
        path: '/update_pics',
        name: 'update_pics',
        component: update_pics,
        beforeEnter: check_login
    },
    {
        path: '/addgoods',
        name: 'addgoods',
        component: addgoods,
        beforeEnter: check_login
    },
    {
        path: '/editgood',
        name: 'editgood',
        component: editgood,
        beforeEnter: check_login
    },
    {
        path: '/search',
        name: 'search',
        component: search,
        beforeEnter: check_login
    }
]

export default new Router({
    routes: routes,
    mode: 'history' /*hash*/
})